# Create your views here.
from datetime import *
from django.shortcuts import render_to_response
from django.db.models import Q, Max, Min, Count
from django.http import HttpResponse,HttpResponseRedirect
from django.template import Context, loader, RequestContext
from django.contrib.auth import logout, authenticate, login 
from django.contrib.auth.models import *
from django.core.mail import send_mail
from django.contrib.auth.decorators import login_required
from registro.forms import *
from registro.models import *
import datetime, string
from django.core.urlresolvers import reverse
from django.views.decorators.csrf import csrf_protect

def login_up(request, error_message=''):
    form = RegisterForm()
    if "user" in request.COOKIES:
        return HttpResponseRedirect('usuarios/panel_inicial.html') 
    if request.method == 'POST':
        form = RegisterForm(request.POST)
        if form.is_valid():
            nombre = form.cleaned_data['nombre']
            apellido = form.cleaned_data['apellido']
            direccion = form.cleaned_data['direccion']
            codigopostal = form.cleaned_data['codigopostal']
            email = form.cleaned_data['email']
            vermail = Clientes.objects.filter(email=email)
            if(int(vermail.count())==0):
                pais = form.cleaned_data['pais']
                telefono = form.cleaned_data['telefono']
                create_user = Clientes(nombre= nombre, 
                               apellido = apellido,
                               direccion = direccion,
                               codigopostal= codigopostal,
                               email = email,
                               pais =  pais,
                               telefono = telefono,
                               )
                create_user.save()
                sesion = SesionesDeCuestionarios(cliente_id=int(create_user.id), fecha = datetime.datetime.now())
                sesion.save()
                response = HttpResponseRedirect('/contestar/')
                #response=render_to_response('contestar/',{'idsesion': sesion.id})
                response.set_cookie("user", int(create_user.id), 365*24*60*60)
                response.set_cookie("sesid", int(sesion.id), 365*24*60*60)
                ElementosAcTemp.objects.all().delete()
                return response
            else:
                return render_to_response('registro/registro.html',{'error_message': 'Ya existe un usuario con ese email','form': form})  
        else:
            return render_to_response('registro/registro.html',{'form': form})
    else:
        return render_to_response('registro/registro.html',{'form': form})

def logout_contestar(request):
    responsen = HttpResponseRedirect('/')
    if "user" in request.COOKIES:
        #responsen = HttpResponseRedirect('/login/')
        responsen.delete_cookie("user")
        if "sesid" in request.COOKIES:
            responsen.delete_cookie("sesid")
        
    return responsen
    #return HttpResponseRedirect('/login/')
